package com.cplatform.movie.back.avtivities.web;

import java.sql.SQLException;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cplatform.dbhelp.page.Page;
import com.cplatform.movie.back.avtivities.dao.ActivitiesDao;
import com.cplatform.movie.back.avtivities.entity.Activities;
import com.cplatform.movie.back.avtivities.service.ActivitiesService;
import com.cplatform.movie.back.model.SessionUser;
import com.cplatform.movie.back.sale.entity.ItemSale;
import com.cplatform.movie.back.utils.JsonRespWrapper;
import com.cplatform.movie.back.utils.LogUtils;

/**
 * 活动控制层. <br>
 * Description.
 * <p>
 * Copyright: Copyright (c) 2015年6月28日 下午12:53:45
 * <p>
 * Author: liuyt@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
@Controller
@RequestMapping("activities")
public class ActivitiesController {
	private Logger log = Logger.getLogger(ActivitiesController.class);
	@Autowired
	private ActivitiesService activitiesService;
	@Autowired
	private LogUtils logUtils;
	@Autowired
	private ActivitiesDao activitiesDao;

	/**
	 * @Title: 进入活动列表页面 <br>
	 * @param ac
	 * @param page
	 * @param model
	 * @return
	 * @throws
	 */
	@RequestMapping("/list")
	public String list(Activities ac, @RequestParam(required = false, value = "page", defaultValue = "1") int page, Model model) {
		Page<Activities> pageData = null;
		try {
			pageData = activitiesService.list(ac, page);
		} catch (SQLException e) {
			log.error("进入活动列表失败：" + e.getMessage());
		}

		model.addAttribute("pageData", pageData);
		model.addAttribute("ac", ac);
		model.addAttribute("auditStatusMap", Activities.auditStatusMap);
		model.addAttribute("saleStatusMap", Activities.saleStatusMap);
		model.addAttribute("typeMap", Activities.typeMap);
		return "activities/activities-list";
	}

	/**
	 * @Title: 进入活动新增页面 <br>
	 * @param model
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addGet(Model model) {
		Activities ac = new Activities();
		model.addAttribute("ac", ac);
		model.addAttribute("typeMap", Activities.typeMap);
		model.addAttribute("uploadImgId", new Date().getTime());
		return "activities/activities-add";
	}

	/**
	 * @Title: 活动新增 <br>
	 * @param model
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Object addPost(Activities ac, HttpSession session, Model model) {
		SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.SESSION_USER_KEY);
		ac = activitiesService.save(ac, sessionUser.getId());
		logUtils.logModify("商品管理", "商品更新成功");
		return JsonRespWrapper.success("活动新增成功", "/activities/list");
	}

	/**
	 * @Title: 进入活动编辑页面 <br>
	 * @param model
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "edit", method = RequestMethod.GET)
	public String editGet(Model model, @RequestParam Long id) {
		Activities ac = activitiesDao.findOne(id);
		model.addAttribute("ac", ac);
		model.addAttribute("typeMap", Activities.typeMap);
		model.addAttribute("uploadImgId", new Date().getTime());
		return "activities/activities-edit";
	}

	/**
	 * @Title: 活动编辑<br>
	 * @param model
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	public Object editPost(Activities ac, Model model) {
		Activities activities = activitiesDao.findOne(ac.getId());
		activitiesService.updete(ac, activities);
		model.addAttribute("ac", ac);
		model.addAttribute("typeMap", Activities.typeMap);
		model.addAttribute("uploadImgId", new Date().getTime());
		return JsonRespWrapper.success("活动更新成功", "/activities/list");
	}

	/**
	 * @Title: 进入活动审核页面 <br>
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/audit")
	public String auditGet(@RequestParam Long id, Model model) {
		Activities ac = null;
		ac = activitiesDao.findOne(id);
		model.addAttribute("ac", ac);
		return "activities/activities-audit";
	}

	/**
	 * @Title: 活动审核 <br>
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/audit", method = RequestMethod.POST)
	@ResponseBody
	public Object auditPost(@RequestParam(required = true) Long id, @RequestParam(required = true) Long auditSataus, Model model) {
		Activities ac = activitiesDao.findOne(id);
		ac.setStatus(auditSataus);
		activitiesDao.save(ac);
		logUtils.logAudit("活动管理", "活动审核成功,id:" + id);
		return JsonRespWrapper.success("商品审核成功", "/activities/list");
	}
	
	/**
	 * @Title: 活动审核 <br>
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/sendAudit")
	@ResponseBody
	public Object sendAudit(@RequestParam(required = true) Long id, Model model) {
		Activities ac = activitiesDao.findOne(id);
		ac.setStatus(1l);
		activitiesDao.save(ac);
		logUtils.logAudit("活动管理", "活动送审成功,id:" + id);
		return JsonRespWrapper.successReload("送审成功");
	}

	/**
	 * @Title: 活动上下架 <br>
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/ruing/{type}/{id}")
	@ResponseBody
	public Object runing(@PathVariable Long id, @PathVariable Long type) {
		Activities ac = activitiesDao.findOne(id);
		if (type == 1) { // 上线
			ac.setIsValid(1l);
		} else if (type == 0) { // 下线
			ac.setIsValid(0l);
		}
		activitiesDao.save(ac);
		logUtils.logOther("活动管理", "活动上/下线成功,id:" + id);
		return JsonRespWrapper.successReload("操作成功");
	}

	/**
	 * @Title: 商品逻辑删除 <br>
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public Object delete(@PathVariable Long id, Model model) {
		Activities ac = activitiesDao.findOne(id);
		ac.setStatus(-1l);
		activitiesDao.save(ac);
		logUtils.logDelete("活动管理", "活动删除成功,id:" + id);
		return JsonRespWrapper.successReload("商品删除成功");
	}
	
	/**
	 * @Title: 商品逻辑删除 <br>
	 * @param id
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "/view/{id}")
	public Object view(@PathVariable Long id, Model model) {
		Activities ac = activitiesDao.findOne(id);
		model.addAttribute("ac", ac);
		model.addAttribute("auditStatusMap", Activities.auditStatusMap);
		model.addAttribute("saleStatusMap", Activities.saleStatusMap);
		model.addAttribute("typeMap", Activities.typeMap);
		return "activities/activities-view";
	}
	
}
